#ifndef MY_INDEV_H
#define MY_INDEV_H

#ifdef __cplusplus
extern "C" {
#endif

#include <Arduino.h>

#define LV_INDEV_STATE_REL LV_INDEV_STATE_RELEASED 
#define LV_INDEV_STATE_PR LV_INDEV_STATE_PRESSED



//按键配置
#define EC11_A_PIN 6
#define EC11_B_PIN 10
#define EC11_K_PIN 9
static const long key_long_press_time = 210;


static int32_t encoder_diff;
static lv_indev_state_t encoder_state;

static void encoder_init(void);
static void encoder_read(lv_indev_t * indev, lv_indev_data_t*data);
static void encoder_handler(void);

void lv_port_indev_init(void) {
  /* Here you will find example implementation of input devices supported by
   * LittelvGL:
   *  - Touchpad
   *  - Mouse (with cursor support)
   *  - Keypad (supports GUI usage only with key)
   *  - Encoder (supports GUI usage only with: left, right, push)
   *  - Button (external buttons to press points on the screen)
   *
   *  The `..._read()` function are only examples.
   *  You should shape them according to your hardware
   */

  /*------------------
   * Encoder
   * -----------------*/

  /*Initialize your encoder if you have*/
  encoder_init();

  
  //创建旋转编码器为输入设备
  /*Register at least one display before you register any input devices*/
  lv_indev_t * indev = lv_indev_create();
  lv_indev_set_type(indev, LV_INDEV_TYPE_ENCODER);   /*See below.*/
  lv_indev_set_read_cb(indev, encoder_read);  /*See below.*/  //绑定输入设备的回调函数
  /* 创建group 后面将控件绑定到组即绑定到输入设备 */
  lv_group_t * group = lv_group_create();
  lv_indev_set_group(indev, group);
}

/*------------------
 * Encoder
 * -----------------*/

/* Initialize your keypad */
static void encoder_init(void) {
  /*Your code comes here*/

  pinMode(EC11_A_PIN, INPUT_PULLUP);
  pinMode(EC11_B_PIN, INPUT_PULLUP);
  pinMode(EC11_K_PIN, INPUT_PULLUP);
}

/*Will be called by the library to read the encoder*/
static void encoder_read(lv_indev_t * indev, lv_indev_data_t*data) {
  encoder_handler();
  data->enc_diff = encoder_diff;
  data->state = encoder_state;

  // Serial.printf("x: %d,y: %d\n", data->enc_diff, data->state);
}





/*Call this function in an interrupt to process encoder events (turn, press)*/
static void encoder_handler(void) {
  encoder_state = LV_INDEV_STATE_REL;
  encoder_diff = 0;

  
  if (digitalRead(EC11_A_PIN) == LOW &&digitalRead(EC11_B_PIN) == HIGH) {
    delay(20);
    if (digitalRead(EC11_A_PIN) == LOW &&digitalRead(EC11_B_PIN) == LOW) {
      encoder_diff = -1;
      encoder_state = LV_INDEV_STATE_REL;
      long last_time = millis();
      while ((digitalRead(EC11_A_PIN) == HIGH && digitalRead(EC11_B_PIN) == LOW || digitalRead(EC11_A_PIN) == LOW && digitalRead(EC11_B_PIN) == HIGH)) {
        if (millis() - last_time > key_long_press_time) break;
      }
  }
  }



  
  if (digitalRead(EC11_B_PIN) == LOW&&digitalRead(EC11_A_PIN) == HIGH ) {
    delay(20);
    if (digitalRead(EC11_B_PIN) == LOW&&digitalRead(EC11_A_PIN) == LOW ){
      encoder_diff = 1;
      encoder_state = LV_INDEV_STATE_REL;
      long last_time = millis();
      while ((digitalRead(EC11_A_PIN) == LOW && digitalRead(EC11_B_PIN) == HIGH) || (digitalRead(EC11_A_PIN) == HIGH && digitalRead(EC11_B_PIN) == LOW)) {
        if (millis() - last_time > key_long_press_time) break;
      }
  }
  }



  if (digitalRead(EC11_K_PIN) == LOW) {
    encoder_state = LV_INDEV_STATE_PR;
  } else if (digitalRead(EC11_K_PIN) == HIGH) {
    encoder_state = LV_INDEV_STATE_REL;
  }


}////////////

#ifdef __cplusplus
} /*extern "C"*/
#endif

#endif /*MY_INDEV_H*/
